<template>
  <div class="user-post">
    <div style="height: 40px">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>个人中心</el-breadcrumb-item>
        <el-breadcrumb-item>帖子信息</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div v-loading="postLoading" element-loading-text="拼命加载中">
      <CommonPostItemView
        :empty="'暂无帖子'"
        :postData="tableData"
      ></CommonPostItemView>
    </div>
    <el-pagination
      v-show="tableData.length > 0"
      layout="prev, pager, next"
      :current-page="currentPage"
      @current-change="handleCurrentChange"
      :total="total"
      :page-count="7"
      style="text-align: right; margin-top: 20px"
    >
    </el-pagination>
  </div>
</template>

<script>
import CommonPostItemView from "@/components/CommonPostItemView.vue";
import { pageQueryUserPost } from "@/api/client/post";
import userModule from "@/utils/userInfo";

export default {
  name: "UserPostView",
  components: {
    CommonPostItemView,
  },
  data() {
    return {
      tableData: [],
      postLoading: false,
      page: 1,
      pageSize: 10,
      total: 0,
      currentPage: 1,
    };
  },
  created() {
    this.getPostByUserId();
  },
  methods: {
    // 获取用户帖子
    async getPostByUserId() {
      // 如果不是请求体的话就直接传值，不需要用键值对形式
      try {
        this.postLoading = true;
        const res = await pageQueryUserPost({
          page: this.page,
          pageSize: this.pageSize,
          userId: userModule.getUser().id,
        });
        this.tableData = res.data.records;
        this.total = res.data.total;
      } finally {
        this.postLoading = false;
      }
    },
    // 分页查询
    handleCurrentChange(page) {
      this.page = page;
      this.currentPage = page;
      this.getPostByUserId();
    },
  },
};
</script>

<style>
</style>